package com.cqjtu.studentdocument.controller;

import com.cqjtu.studentdocument.advice.ExceptionEnums;
import com.cqjtu.studentdocument.advice.MyException;
import com.cqjtu.studentdocument.dao.CheckInfoDao;
import com.cqjtu.studentdocument.dao.UserDao;
import com.cqjtu.studentdocument.entity.CheckInfo;
import com.cqjtu.studentdocument.service.CheckInfoService;
import com.cqjtu.studentdocument.utils.dto.AnalysisData;
import com.cqjtu.studentdocument.utils.dto.HealthDTO;
import com.cqjtu.studentdocument.utils.vo.AnalysisChoose;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(description = "体检表信息接口")
@Controller
@RequestMapping(value = "api/checkInfo")
public class CheckInfoController extends BaseController<CheckInfoService, CheckInfo, Integer> {


    @Resource
    CheckInfoDao checkInfoDao;
    @Resource
    UserDao userDao;


    @Override
    @RequiresPermissions("checkInfo:add")
    public ResponseEntity<CheckInfo> save(@RequestBody CheckInfo entity) {
        return super.save(entity);
    }

    @Override
    @RequiresPermissions("checkInfo:update")
    public ResponseEntity<CheckInfo> update(@RequestBody CheckInfo entity) {
        return super.update(entity);
    }

    @Override
    @RequiresPermissions("checkInfo:delete")
    @GetMapping(value = "delete/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Integer integer) {
        return super.delete(integer);
    }

    /**
     * 判断体检表是否存在
     *
     * @param userId    用户id
     * @param checkYear 检查年份
     * @return 是否存在
     */
    @GetMapping("/judgeCheckIsExist")
    ResponseEntity judgeCheckIsExist(Integer userId, String checkYear) {
        Example example = new Example(CheckInfo.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("checkYear", checkYear);
        List<CheckInfo> checkInfos = this.service.selectByExample(example);
        if (checkInfos == null || checkInfos.size() == 0) {
            return ResponseEntity.ok(true);
        } else {
            return ResponseEntity.ok(false);
        }
    }

    @GetMapping("/getBim")
    ResponseEntity judgeIsHealth(Double height, Double weight) {
        String suggestion;
        Double result = weight / ((height / 100) * (height / 100));
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.UP);
        result = Double.valueOf(nf.format(result));
        if (result < 19) {
            suggestion = "体重偏低";
        } else if (result < 25) {
            suggestion = "健康体重";
        } else if (result < 30) {
            suggestion = "超重";
        } else if (result < 39) {
            suggestion = "严重超重";
        } else {
            suggestion = "极度超重";
        }
        return ResponseEntity.ok(new HealthDTO().setBim(result).setSuggestion(suggestion));
    }

    @GetMapping("/getDataAnalysis/{userId}")
    ResponseEntity getDataAnalysis(@PathVariable("userId") Integer userId) {
        List<CheckInfo> analysis = checkInfoDao.getDataAnalysis(userId);
        if (analysis == null) {
            throw new MyException(ExceptionEnums.NO_CHECK_INFO);
        } else {
            List<String> label = new ArrayList<>();
            List<Double> height = new ArrayList<>();
            List<Double> weight = new ArrayList<>();
            analysis.forEach(v -> {
                label.add(v.getCheckYear());
                height.add(v.getHeight());
                weight.add(v.getWeight());
            });
            return ResponseEntity.ok(new AnalysisData().setLabel(label).setHeight(height).setWeight(weight));
        }
    }

    @GetMapping("/getCurrentCheckInfo/{userId}")
    ResponseEntity<CheckInfo> getCurrentCheckInfo(@PathVariable("userId") Integer userId) {
        CheckInfo checkInfo = checkInfoDao.getCurrentCheckInfo(userId);
        if (checkInfo == null) {
            throw new MyException(ExceptionEnums.NO_CHECK_INFO);
        } else {
            return ResponseEntity.ok(checkInfo);
        }
    }

    @GetMapping("/getStudyYear")
    ResponseEntity<List<CheckInfo>> getStudyYear() {
        List<CheckInfo> checkInfoList = checkInfoDao.getStudyYear();
        if (checkInfoList == null) {
            throw new MyException(ExceptionEnums.GET_LIST_ERROR);
        } else {
            return ResponseEntity.ok(checkInfoList);
        }
    }

    @GetMapping("/getCollege")
    ResponseEntity<List<CheckInfo>> getCollege() {
        List<CheckInfo> checkInfoList = checkInfoDao.getCollege();
        if (checkInfoList == null) {
            throw new MyException(ExceptionEnums.GET_LIST_ERROR);
        } else {
            return ResponseEntity.ok(checkInfoList);
        }
    }

    @GetMapping("/analysisAll/{studyYear}")
    ResponseEntity<Map<String, Object>> analysisAll(@PathVariable String studyYear) {
        Map<String, Object> analysisList = service.analysisAll(studyYear);
        if (analysisList == null) {
            throw new MyException(ExceptionEnums.GET_LIST_ERROR);
        } else {
            return ResponseEntity.ok(analysisList);
        }
    }

    @PostMapping("/analysis")
    ResponseEntity<Map<String, Object>> analysis(@RequestBody AnalysisChoose analysisChoose) {
        Map<String, Object> analysisList = service.analysis(analysisChoose);
        if (analysisList == null) {
            throw new MyException(ExceptionEnums.GET_LIST_ERROR);
        } else {
            return ResponseEntity.ok(analysisList);
        }
    }


}

